package com.swt.dao.impl.W_impl;

import com.swt.dao.W_dao.StatsDao;
import com.swt.util.GetCurrentTime;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Repository
public class StatsDaoImpl implements StatsDao {
    @Resource
    private HibernateTemplate hibernateTemplate;

    /**
     * 查询用户每月注册数量
     * */
    public List<Map<String, Object>> countUser(){
        String sql="SELECT user_regmonth month,COUNT(*) num FROM user GROUP BY user_regmonth";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        System.out.println(query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    /**
     *查询商家每月注册数量
     * */
    public List<Map<String, Object>> countClub(){
        String sql="SELECT club_regmonth month,COUNT(*) num FROM club GROUP BY club_regmonth";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        System.out.println(query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //用户今日注册人数
    @Override
    public List<Map<String, Object>> countUserDay() {
        String sql="select count(*) num from user where user_regdate=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getToday());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //用户昨日注册人数
    @Override
    public List countUserYesterday() {
        String sql="select count(*) num from user where user_regdate=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getYesterday());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //用户本周注册人数
    @Override
    public List countUserWeek() {
        String sql="select count(*) num from user where user_regdate>=? and user_regdate<=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getWeekStart());
        query.setParameter(1,GetCurrentTime.getWeekEnd());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //用户本月注册人数
    @Override
    public List countUserMonth() {
        String sql="select count(*) num from user where user_regmonth=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getMonth());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //商家今日注册人数
    @Override
    public List countClubDay() {
        String sql="select count(*) num from club where club_regdate=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getToday());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //商家昨日注册人数
    @Override
    public List countClubYesterday() {
        String sql="select count(*) num from club where club_regdate=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getYesterday());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //商家本周注册人数
    @Override
    public List countClubWeek() {
        String sql="select count(*) num from club where club_regdate>=? and club_regdate<=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getWeekStart());
        query.setParameter(1,GetCurrentTime.getWeekEnd());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //商家本月注册人数
    @Override
    public List countClubMonth() {
        String sql="select count(*) num from club where club_regmonth=?";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        query.setParameter(0,GetCurrentTime.getMonth());
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }


    //用户注册总数
    public List countUserAll(){
        String sql="select count(*) num from user";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }

    //商家注册总数
    public List countClubAll(){
        String sql="select count(*) num from club";
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.createSQLQuery(sql);
        return query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
    }


}
